package com.xh.baselibrary.log

import android.text.TextUtils
import android.util.Log

object XLog {
    const val TAG = "XLog"
    @JvmStatic
    fun i(content: String) {
        Log.i(getStackInfo(4, TAG), content)
    }
    @JvmStatic
    fun i(tag: String, content: String) {
        Log.i(getStackInfo(4, tag), content)
    }
    @JvmStatic
    fun w(content: String) {
        Log.w(getStackInfo(4, TAG), content)
    }
    @JvmStatic
    fun w(tag: String, content: String) {
        Log.w(getStackInfo(4, tag), content)
    }
    @JvmStatic
    fun d(content: String) {
        Log.d(getStackInfo(4, TAG), content)
    }
    @JvmStatic
    fun d(tag: String, content: String) {
        Log.d(getStackInfo(4, tag), content)
    }
    @JvmStatic
    fun e(content: String) {
        Log.e(getStackInfo(4, TAG), content)
    }
    @JvmStatic
    fun e(tag: String, content: String) {
        Log.e(getStackInfo(4, tag), content)
    }
    @JvmStatic
    fun v(content: String) {
        Log.v(getStackInfo(4, TAG), content)
    }
    @JvmStatic
    fun v(tag: String, content: String) {
        Log.v(getStackInfo(4, tag), content)
    }
    @JvmStatic
    private fun getStackInfo(stackIndex: Int, tag: String?): String {
        if (tag != null && tag.startsWith("[") && tag.endsWith("]")) return tag
        val buffer = StringBuilder()
        val stackTrace = Thread.currentThread().stackTrace.run { if (stackIndex < size) this[stackIndex] else null }
        buffer.append("[")
        buffer.append(Thread.currentThread().id)
        stackTrace?.let {
            buffer.append(":${it.methodName}(${it.fileName}:${it.lineNumber})")
        }
        if (!TextUtils.isEmpty(tag)) {
            buffer.append(":$tag")
        }
        buffer.append("]")
        return buffer.toString()
    }
}